package com.infore.siteManagement.export.service.lmpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.infore.siteManagement.export.service.IExportService;
import com.infore.siteManagement.export.util.ExportUtil;
import com.infore.siteManagement.select.dto.QueryParamDto;
import com.infore.siteManagement.select.util.PageObject;
import com.infore.siteManagement.service.EquipmentService;
import com.infore.siteManagement.service.StationService;
import com.infore.siteManagement.service.equipment.IEquipReagentService;
import com.infore.siteManagement.util.ObjectMapperUtil;
import com.infore.siteManagement.util.ResultEntity;

@Service
public class ExportServiceImpl implements IExportService {

	@Autowired
	private EquipmentService equipmentService;
	@Autowired
	ObjectMapperUtil ObjectMapperUtil;
	@Autowired
	private StationService stationService;
	@Autowired
	private IEquipReagentService iEquipReagentService;

	public byte[] exportExcelData(HttpServletRequest request) throws Exception {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		String stationCodes = paramsJson.getString("stationCodes");
		String pattern = paramsJson.getString("pattern"); // 时间类型
		PageObject page = null;
		ResultEntity resultEntity = equipmentService.queryInstrumentParameters(stationCodes,page);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list = ObjectMapperUtil.convertListMap(resultEntity);
		return ExportUtil.exportExcel(request, list, headerProp, pattern);
	}

	public byte[] exportStation(HttpServletRequest request) {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		String areaId = paramsJson.getString("areaId");
		String stationName = paramsJson.getString("stationName");
		@SuppressWarnings("unused")
		String roleId = paramsJson.getString("roleId");
		String pattern = paramsJson.getString("pattern"); // 时间类型
		PageObject page = null;
		ResultEntity resultEntity = stationService.singleStationInfo(areaId,stationName,page,request);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list = ObjectMapperUtil.convertListMap(resultEntity);
		return ExportUtil.exportExcel(request, list, headerProp, pattern);
	}

	public byte[] reagentExcel(HttpServletRequest request) {
		String params = request.getParameter("params").toString(); // 额外参数
		JSONObject paramsJson = JSONObject.parseObject(params);
		String headerProp = paramsJson.getString("headerProp"); // 数据映射属性名
		String pattern = paramsJson.getString("pattern"); // 时间类型
		QueryParamDto queryParamDto = paramsJson.getObject("queryParam", QueryParamDto.class);
		ResultEntity resultEntity = iEquipReagentService.query(queryParamDto);
		List<Map<String, Object>> list = ObjectMapperUtil.convertListMap(resultEntity);
		return ExportUtil.exportExcel(request, list, headerProp, pattern);
	}

}
